$(function() {
    let layer = layui.layer
    let form = layui.form
    let laypage = layui.laypage;
    //申明q查询对象
    let q = {
        pagenum: 1,
        pagesize: 2,
        cate_id: '',
        state: ''
    }
    initArtList()

    //渲染文章列表
    function initArtList() {
        $.ajax({
            method: 'get',
            url: '/my/article/list',
            data: q,
            success: function(res) {
                if (res.code !== 0) {
                    return layer.msg('获取文章列表失败！')
                }
                let htmlStr = template('tpl_table', res)
                $('tbody').html(htmlStr)
                renderPage(res.total)
            }
        })
    }
    // 时间过滤函数
    template.defaults.imports.dataFormat = function(date) {
        const dt = new Date(date)

        var y = dt.getFullYear()
        var m = padZero(dt.getMonth() + 1)
        var d = padZero(dt.getDate())

        var hh = padZero(dt.getHours())
        var mm = padZero(dt.getMinutes())
        var ss = padZero(dt.getSeconds())

        return y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss
    }

    // 定义补零的函数
    function padZero(n) {
        return n > 9 ? n : '0' + n
    }
    initCate()


    function initCate() {
        $.ajax({
            method: 'get',
            url: '/my/cate/list',
            success: function(res) {
                if (res.code !== 0) {
                    return layer.msg('获取文章分类列表失败！')
                }
                let htmlStr = template('tpl_cateList', res)
                $('[name=cate_id]').html(htmlStr)
                form.render()
            }
        })
    }
    $('#tpl_select').on('submit', function(e) {
        e.preventDefault()
        let cate_id = $('[name=cate_id]').val()
        let state = $('[name=state]').val()
        q.cate_id = cate_id
        q.state = state
        initArtList()
    })

    function renderPage(total) {
        laypage.render({
            elem: 'pageBox', //注意，这里的 test1 是 ID，不用加 # 号

            count: total, //数据总数，从服务端得到
            curr: q.pagenum,
            limit: q.pagesize,
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 3, 5, 10],
            jump: function(obj, first) {
                q.pagenum = obj.curr
                q.pagesize = obj.limit
                if (!first) {
                    initArtList()
                }
            }
        });
    }
    $('tbody').on('click', '.btn_del', function() {
        let id = $(this).attr('data-id')
        let len = $('.btn_del').length
        layer.confirm('确认是否删除?', { icon: 3, title: '提示' }, function(index) {
            $.ajax({
                method: 'delete',
                url: '/my/article/info?id=' + id,
                success: function(res) {
                    if (res.code !== 0) {
                        return layer.msg('文章删除失败！')
                    }
                    layer.msg('文章删除成功！')
                    if (len == 1) {
                        q.pagenum = q.pagenum === 1 ? q.pagenum : q.pagenum - 1
                    }
                    initArtList()
                }
            })

            layer.close(index);
        });

    })
})